<?php
function _GetProductName($idproduct)
{
	$sql = "SELECT * FROM tbl_product WHERE id = '$idproduct'";
	$result = mysql_query("$sql");
	if(mysql_num_rows($result) > 0)
	{
		while($rows = mysql_fetch_assoc($result))
		{
			extract($rows);
		}
		$proname = $pro_name;
		return $proname;
	}
	else
	{
		echo "invaild product id";
	}
}
function _GetNewsTitle($idNews)
{
	$sql = "SELECT * FROM tbl_news WHERE id = '$idNews'";
	$result = mysql_query("$sql");
	if(mysql_num_rows($result) > 0)
	{
		while($rows = mysql_fetch_assoc($result))
		{
			extract($rows);
		}
		$news = $news_title;
		return $news;
	}
	else
	{
		echo "invaild news id";
	}
}
function _GetCatName($idcategory=1)
{
	$sql = "SELECT * FROM tbl_category WHERE id = '$idcategory'";
	$result = mysql_query("$sql");
	if(mysql_num_rows($result) > 0)
	{
		while($rows = mysql_fetch_assoc($result))
		{
			extract($rows);
		}
		$catname = $name;
		return $catname;
	}
	else
	{
		echo "invaild category id";
	}
			
}
function _GetCatList($idcategory) //this function return list category with selected option from id category input
{
	$cat_sql = "SELECT * FROM tbl_category";
	$cat_result = mysql_query("$cat_sql") or die("can not load category");
	$cat = "";
	if(mysql_num_rows($cat_result) > 0)
	{
		while($rows = mysql_fetch_assoc($cat_result))
		{
			extract($rows);
			if($id == $idcategory)
			{
				$cat .= "<option value='$id' selected='selected'>$name</option>\r\n";
			}
			else
			{
				$cat .=	"<option value='$id'>$name</option>\r\n";
			}
		}
	return $cat;
	}
	else
	{
		echo "Can not query";	
	}
}

function paginate ($base_url, $query_str, $total_pages, 
                    $current_page, $paginate_limit)
{
    // Array to store page link list
    $page_array = array ();
    // Show dots flag - where to show dots?
    $dotshow = true;
    // walk through the list of pages
    for ( $i = 1; $i <= $total_pages; $i ++ )
    {
       // If first or last page or the page number falls 
       // within the pagination limit
       // generate the links for these pages
       if ($i == 1 || $i == $total_pages || 
             ($i >= $current_page - $paginate_limit && 
             $i <= $current_page + $paginate_limit) )
       {
          // reset the show dots flag
          $dotshow = true;
          // If it's the current page, leave out the link
          // otherwise set a URL field also
          if ($i != $current_page)
              $page_array[$i]['url'] = $base_url . "&" . $query_str .
                                         "=" . $i;
          $page_array[$i]['text'] = strval ($i);
       }
       // If ellipses dots are to be displayed
       // (page navigation skipped)
       else if ($dotshow == true)
       {
           // set it to false, so that more than one 
           // set of ellipses is not displayed
           $dotshow = false;
           $page_array[$i]['text'] = "...";
       }
    }
    // return the navigation array
    return $page_array;
}

function _totalpage($sql, $itemPerPage)
{
	$result = mysql_query($sql);
	$totalResult = mysql_num_rows($result);
	$totalPage = ceil($totalResult/$itemPerPage);
	return $totalPage;	
}

function _getPagingQuery($sql, $itemPerPage = 10)
{
	if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
		$page = (int)$_GET['page'];
	} else {
		$page = 1;
	}
	
	// start fetching from this row number
	$offset = ($page - 1) * $itemPerPage;
	
	return $sql . " LIMIT $offset, $itemPerPage";
}

function _getCurrentPageURL() {
    $pageURL = 'http';
    if (!empty($_SERVER['HTTPS'])) {if($_SERVER['HTTPS'] == 'on'){$pageURL .= "s";}} 
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

function _GetSetting()
{
	$sql = "SELECT * FROM tbl_config";
	$result = mysql_query("$sql");
	if(mysql_num_rows($result) > 0)
	{
		while($rows = mysql_fetch_assoc($result))
		{
			extract($rows);
		}
		$name = array('name' =>$res_name, 'about' => $res_aboutus, 'tos' => $res_tos, 'cpy' => $res_copyright);
		return $name;
	}
	else
	{
		echo "Error with get setting";
	}
			
}
?>